package com.swas.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageHelper;
import com.swas.dao.PatrolPlanMapper;
import com.swas.entity.PatrolTaskDTO;
import com.swas.entity.*;
import com.swas.dao.PatrolTaskMapper;
import com.swas.dao.TaskPointDao;
import com.swas.service.PatrolTaskService;
import com.swas.vo.PatrolTaskVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class PatrolTaskServiceImpl implements PatrolTaskService {

    @Autowired
    private PatrolTaskMapper patrolTaskMapper;

    @Autowired
    private PatrolPlanMapper patrolPlanMapper;

    @Autowired
    private TaskPointDao taskPointDao;

    @Override
    public List<PatrolTask> list(PatrolTaskDTO patrolTaskDTO, User userInfo) {
        PageHelper.startPage(patrolTaskDTO.getPageNum(), patrolTaskDTO.getPageSize());
        PatrolTaskExample example = new PatrolTaskExample();
        PatrolTaskExample.Criteria criteria = example.createCriteria();
        if (patrolTaskDTO.getTaskno()!=null&&!"".equals(patrolTaskDTO.getTaskno())) {
            criteria.andTasknoEqualTo(patrolTaskDTO.getTaskno());
        }
        if (patrolTaskDTO.getTaskname()!=null&&!"".equals(patrolTaskDTO.getTaskname())) {
            criteria.andTasknameLike("%"+patrolTaskDTO.getTaskname()+"%");
        }
        if (patrolTaskDTO.getTasktype()!=null&&!"".equals(patrolTaskDTO.getTasktype())) {
            criteria.andTasktypeEqualTo(patrolTaskDTO.getTasktype());
        }
        if (patrolTaskDTO.getTaskstatu()!=null) {
            criteria.andTaskstatuEqualTo(patrolTaskDTO.getTaskstatu());
        }
        if (patrolTaskDTO.getStartdate() != null) {

            if (patrolTaskDTO.getStartdate().get(0) != null && patrolTaskDTO.getStartdate().get(1) != null) {
                criteria.andStartdateBetween(patrolTaskDTO.getStartdate().get(0), patrolTaskDTO.getStartdate().get(1));
            } else if (patrolTaskDTO.getStartdate().get(0) != null) {
                criteria.andStartdateGreaterThanOrEqualTo(patrolTaskDTO.getStartdate().get(0));
            } else {
                criteria.andStartdateLessThanOrEqualTo(patrolTaskDTO.getStartdate().get(1));
            }
        }
        if (patrolTaskDTO.getEnddate() != null) {

            if (patrolTaskDTO.getEnddate().get(0) != null && patrolTaskDTO.getEnddate().get(1) != null) {
                criteria.andEnddateBetween(patrolTaskDTO.getEnddate().get(0), patrolTaskDTO.getEnddate().get(1));
            } else if (patrolTaskDTO.getEnddate().get(0) != null) {
                criteria.andEnddateGreaterThanOrEqualTo(patrolTaskDTO.getEnddate().get(0));
            } else {
                criteria.andEnddateLessThanOrEqualTo(patrolTaskDTO.getEnddate().get(1));
            }
        }
        if (userInfo != null) {
            criteria.andExecutorEqualTo(userInfo.getUsername());
        }
//        List<PatrolTask> list = patrolTaskMapper.selectByExample(example);
        List<PatrolTask> list = patrolTaskMapper.selectByExample(null);
        return list;
    }

    @Override
    public void update(PatrolTaskDTO patrolTaskDTO) {
        PatrolTask patrolTask = new PatrolTask();
        BeanUtil.copyProperties(patrolTaskDTO,patrolTask);
        patrolTaskMapper.updateByPrimaryKeySelective(patrolTask);
    }

    @Override
    public void taskFP(Integer planId, List<PatrolTaskDTO> patrolTaskDTOList, User userInfo) {
        //任务表删除
        PatrolTaskExample example = new PatrolTaskExample();
        PatrolTaskExample.Criteria criteria = example.createCriteria();
        criteria.andPlanidEqualTo(planId);
        patrolTaskMapper.deleteByExample(example);

        PatrolPlan patrolPlan = patrolPlanMapper.selectByPrimaryKey(planId);
        for (PatrolTaskDTO patrolTaskDTO : patrolTaskDTOList) {
            PatrolTask patrolTask = new PatrolTask();
            patrolTask.setTaskno(patrolPlan.getPlanno());
            patrolTask.setTaskname(patrolPlan.getPlanname());
            patrolTask.setTasktype(patrolPlan.getPattype() == 0 ? "plan" : "temp");
            patrolTask.setTaskstatu(0);
            patrolTask.setExecutor(patrolTaskDTO.getExecutor());
            patrolTask.setStartdate(patrolPlan.getStartdate());
            patrolTask.setEnddate(patrolPlan.getEnddate());
            patrolTask.setDispatchtime(new Date());
            if (userInfo == null){
                patrolTask.setDispatcher("");
            } else {
                patrolTask.setDispatcher(userInfo.getUsername());
            }
            patrolTask.setPlanid(planId);
            patrolTaskMapper.insertSelective(patrolTask);
            List<Integer> pointIdList = patrolTaskDTO.getPointIdList();
            for (Integer pointId : pointIdList) {
                TaskPoint taskPoint = new TaskPoint();
                taskPoint.setPointid(pointId);
                taskPoint.setTaskid(patrolTask.getId());
                taskPointDao.insertSelective(taskPoint);
            }
        }
    }

    @Override
    public List<PatrolTaskVO> getByPlanId(Integer planId) {
        List<PatrolTaskVO> patrolTaskVOList = new ArrayList<PatrolTaskVO>();
        PatrolTaskExample example = new PatrolTaskExample();
        PatrolTaskExample.Criteria criteria = example.createCriteria();
        criteria.andPlanidEqualTo(planId);
        List<PatrolTask> patrolTaskList = patrolTaskMapper.selectByExample(example);
        for (PatrolTask patrolTask : patrolTaskList) {
            PatrolTaskVO patrolTaskVO = new PatrolTaskVO();
            patrolTaskVO.setExecutor(patrolTask.getExecutor());

            Integer taskId = patrolTask.getId();
            TaskPointExample taskPointExample = new TaskPointExample();
            taskPointExample.createCriteria().andTaskidEqualTo(taskId);
            List<TaskPoint> taskPoints = taskPointDao.selectByExample(taskPointExample);
            patrolTaskVO.setPointIdList(taskPoints.stream().map(item -> item.getPointid()).collect(Collectors.toList()));

            patrolTaskVOList.add(patrolTaskVO);
        }
        return patrolTaskVOList;
    }
}
